adata_dir='N:\AnalyzedData\georg\';
tmp_data_dir='E:\RawData\';
%layer 2
%ExpIDs=[5735 5771 6424 6445 6734 6784 6962 6976 6982 7327 7329 7440 7522 7532 7538 7544];

layer=2;

[expxlsnum,expxlstxt]=xlsread('P:\LabLog\Experiments.xls');
ExpIDs=expxlsnum(expxlsnum(:,8)==1&expxlsnum(:,7)==layer,1);


%layer 5
%ExpIDs=[10147 10251];

act_ind=[];
cell_cnt=0;
active_cells=[];
panova=[];
mcorr=[];
cell_id=[];
running_hist=[];
type_fract=[];
saccade_times={};

for ind=1:length(ExpIDs)
    ExpID=ExpIDs(ind);
    disp('--------------------------------------------')
    disp(['Now analyzing Exp ' num2str(ExpID) ' - ' num2str(ind) ' of ' num2str(length(ExpIDs))]);
    disp('--------------------------------------------')
    
    [adata_file,mouse_id,date_id]=find_adata_file(ExpID,adata_dir);
    load([adata_dir mouse_id '\' date_id '\' adata_file]);
    
    aux_data=[];
    imeta_data=[];
    idata=[];
    nbr_iframes=[];
    for knd=1:length(aux_files)
        disp(['loading file ' aux_files{knd} ' from local tmpdata directory']);
        curr_load_path=[tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{knd}];
        tmp_data=load_lvd(curr_load_path);
        aux_data(:,end+1:end+length(tmp_data))=tmp_data;
        
        curr_load_path=[tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{knd}(1:end-4) '.eye'];
        [tmp_idata, tmp_imeta_data] = load_eye_monitor_data(curr_load_path,1);
        nbr_iframes(knd)=length(tmp_imeta_data);
        imeta_data(:,end+1:end+length(tmp_imeta_data))=tmp_imeta_data;
        if knd==1||sum(size(idata(:,:,1))==size(tmp_idata(:,:,1)))==2
            idata(:,:,end+1:end+size(tmp_idata,3))=tmp_idata;
        else
            disp('Idata sizes don''t match - will improvise');
            idata(1:size(tmp_idata,1),1:size(tmp_idata,2),end+1:end+size(tmp_idata,3))=tmp_idata;
        end
    end
    
    frame_channel=3;
    
    aux_data=fix_aux_data_M_PB(aux_data,ExpID);
    frame_times=get_frame_times(aux_data(3,:));
    [velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
    type=(velP_ind&velM_ind)+2*(velP_ind&~velM_ind)+3*(~velP_ind&velM_ind);
    %type=circshift(type,1+round((length(type)-1)*rand));
    
    
    %%% saccade analysis %%%
    if ExpID==7440
        idata=idata(end-249:end,1:250,:);
    end
    [saccade_frames,~]=find_saccades(idata);
    iframe_times=get_iframe_times(imeta_data,aux_data(frame_channel+1,:),nbr_iframes);
    saccade_times{ind,1}=iframe_times(saccade_frames);
    saccade_times{ind,2}=type(round(iframe_times(saccade_frames)/10));
    
    
    running_hist(ind,:)=hist(velP_raw,[-2:0.01:+2]);
    type_fract(ind,:)=hist(type,[0:3])/length(type);

    
    
% % %     for sel_roi=1:length(ROIs)
% % %         %if strcmp(ROIs(sel_roi).type,'d')
% % %             cell_cnt=cell_cnt+1;
% % %             
% % %             act=ftfil(ROIs(sel_roi).activity,18,0,10);
% % %             act=psmooth(act);
% % %             bin_act=act/median(act);
% % %             active_cells(cell_cnt)=sum(bin_act>1.5)>20;
% % %             panova(cell_cnt)=anova1(bin_act,type,'off');
% % %             [r,p]=corrcoef(bin_act(velP_ind(round(frame_times/10))),dy(velP_ind(round(frame_times/10))));
% % %             mcorr(cell_cnt,:)=[r(2) p(2)];
% % %             %bin_act=bin_act>1.5;
% % %             cell_id(:,cell_cnt)=[ExpID,sel_roi];
% % %             %bin_act=bin_act.*[0 double(diff(act)>0)];
% % %             for knd=1:4
% % %                 act_ind(knd,cell_cnt)=mean(bin_act(type(round(frame_times/10))==knd-1));
% % %             end
% % %         %end
% % %     end
end

%modulated_cells=sum(abs(diff(act_ind))>0.05)>0;
[~,b]=max(act_ind(:,logical(active_cells)));
figure;hist(b,[1:0.5:4])

ind_to_exclude=1179;

if 0
    act_ind(:,ind_to_exclude)=[];
    active_cells(ind_to_exclude)=[];
    panova(ind_to_exclude)=[];
end

%act_ind_ao=act_ind(:,find(max(act_ind)>1.05));
act_ind_ao=act_ind(:,:);

cases_list={1,2,3,4,[1 2],[1 3],[1 4],[2 3],[2 4],[3 4]};
act_type=[];
for ind=1:size(act_ind_ao,2)
    for knd=1:length(cases_list)
        act_type(knd,ind)=mean(act_ind_ao(cases_list{knd},ind))/mean(act_ind_ao(setdiff([1:4],cases_list{knd}),ind));
    end
end

[~,act_type]=max(act_type);

sort_ind=[];
for ind=1:10
    curr_inds=find(act_type==ind);
    [~,curr_order]=sort(mean(act_ind_ao(cases_list{ind},curr_inds),1));
    sort_ind(end+1:end+length(curr_inds))=curr_inds(curr_order);
end


figure;imagesc((act_ind_ao(:,sort_ind)))


vdrive=mean(act_ind([2 4],:))./mean(act_ind([1],:));
mdrive=mean(act_ind([2 3],:))./mean(act_ind([1],:));
vedrive=act_ind(3,:)./mean(act_ind([1],:));












